package com.building.hcrm.wcf;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;

import com.building.hcrm.base.BaseParam;
import com.building.hcrm.base.BaseServiceProxy;
import com.building.hcrm.common.ComConst;
import com.building.hcrm.common.DateDeserializer;
import com.building.hcrm.common.HttpHelper;
import com.building.hcrm.model.DoSignResult;
import com.building.hcrm.model.PlanDetail;
import com.building.hcrm.model.PlanExamine;
import com.building.hcrm.model.SignLog;
import com.building.hcrm.model.SignParam;
import com.building.hcrm.model.SignQuery;
import com.building.hcrm.model.SysUser;
import com.building.hcrm.model.SysUserParam;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class TaskServiceProxy extends BaseServiceProxy
{
	private static TaskServiceProxy _instance;

	public static TaskServiceProxy getInstance(Context context)
	{
		if (_instance == null)
		{
			_instance = new TaskServiceProxy();
		}
		_instance._context = context;
		return _instance;
	}

	public Boolean TaskCreate(PlanDetail param) throws Exception
	{
		Boolean re = false;
		GsonBuilder gsonb = new GsonBuilder();
		DateDeserializer dds = new DateDeserializer();
		gsonb.registerTypeAdapter(Date.class, dds);
		Gson gson = gsonb.create();
		String strPost = gson.toJson(param);
		strPost  = String.format("{\"model\":%1$s}", strPost);
		String strReturn = HttpHelper.getInstance(_context).HttpPost(ComConst.SERVICE_URL, "TaskCreate",
				strPost);
		if (ValidateResult(strReturn))
		{
			List<String> lstRe = ProcessResult(strReturn, "TaskCreateResult");
			for(String str : lstRe)
			{
				if(null != str && "true".equals(str.toLowerCase()))
				{
					re = true;
				}
			}
		}
		return re;
	}
	
	public List<PlanDetail> TaskSelfList(BaseParam param) throws Exception
	{
		List<PlanDetail> re = null;

		GsonBuilder gsonb = new GsonBuilder();
		DateDeserializer dds = new DateDeserializer();
		gsonb.registerTypeAdapter(Date.class, dds);
		Gson gson = gsonb.create();
		String strPost = gson.toJson(param);
		String strReturn = HttpHelper.getInstance(_context).HttpPost(ComConst.SERVICE_URL, "TaskSelfList",
				strPost);
		if (ValidateResult(strReturn))
		{
			List<String> lstRe = ProcessResult(strReturn, "TaskSelfListResult");
			for(String str : lstRe)
			{
				re = new ArrayList<PlanDetail>();
				JSONArray ja = new JSONArray(str);
				for (int i = 0; i < ja.length(); i++) 
				{
					PlanDetail pd = new PlanDetail();
					pd = gson.fromJson(ja.getString(i), pd.getClass());
					re.add(pd);
				}
			}
		}
		return re;
	}
	
	public Boolean TaskChangeState(PlanExamine param) throws Exception
	{
		Boolean re = false;
		GsonBuilder gsonb = new GsonBuilder();
		DateDeserializer dds = new DateDeserializer();
		gsonb.registerTypeAdapter(Date.class, dds);
		Gson gson = gsonb.create();
		String strPost = gson.toJson(param);
		strPost  = String.format("{\"model\":%1$s}", strPost);
		String strReturn = HttpHelper.getInstance(_context).HttpPost(ComConst.SERVICE_URL, "TaskChangeState",
				strPost);
		if (ValidateResult(strReturn))
		{
			List<String> lstRe = ProcessResult(strReturn, "TaskChangeStateResult");
			for(String str : lstRe)
			{
				if(null != str && "true".equals(str.toLowerCase()))
				{
					re = true;
				}
			}
		}
		return re;
	}
}
